package com.zzy.librarytest.web;

import com.zzy.librarytest.entity.Book;
import com.zzy.librarytest.entity.Record;
import com.zzy.librarytest.service.impl.BookServviceImpl;
import com.zzy.librarytest.service.impl.RecordServiceImpl;
import com.zzy.librarytest.service.impl.ReturnRecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * 记录操作
 */
@Controller
public class RecordController {
    @Autowired
    private RecordServiceImpl recordService;
    @Autowired
    private BookServviceImpl bookServvice;
    @Autowired
    private ReturnRecordServiceImpl returnRecordService;

    @GetMapping("/homepage/addrecordbook")
    public String selectBook(Integer id,Model model){
        Book book = bookServvice.selectBook(id);
        if (book!=null){
            model.addAttribute("book",book);
            return "record/addrecord";
        }else {
            model.addAttribute("msg","编号为:"+id+" 书不存在");
            return "record/addrecord";
        }
    }

    @PostMapping("/homepage/addrecord")
    public String addrecord(Record record, Model model){
        Integer integer = recordService.insertRecord(record);
        if (integer!=null){
            model.addAttribute("msg","出借成功");
            return "record/addrecord";
        }else {
            model.addAttribute("msg","添加失败");
            return "record/addrecord";

        }
    }

    @GetMapping("/record/returnbook")
    public String returnbook(Model model) {
        List<Record> records = recordService.selectAllRecord();
        model.addAttribute("records",records);
        return "record/returnbook";
    }

    @GetMapping("/homepage/returnoperation")
    public String returnoperation(Integer id,String returntime,Model model) {
        Record record = recordService.selectOneRecord(id);
        if (record!=null){
            record.setReturntime(returntime);
            returnRecordService.insertReturnRecord(record);
            Integer ok = recordService.deleteRecord(id);
            model.addAttribute("msg","归还成功");
            List<Record> records = recordService.selectAllRecord();
            model.addAttribute("records",records);
        }else {
            model.addAttribute("msg","没有这条记录");
        }
        return "record/returnbook";
    }

    @GetMapping("/record/allretrunrecord")
    public String allretrunrecord(Model model) {
        List<Record> records = returnRecordService.selectAllReturnRecord();
        model.addAttribute("records",records);
        return "record/allretrunrecord";
    }

    @GetMapping("/homepage/returnoperationa")
    public String returnoperationa(Integer id, Model model) {
        Record record = returnRecordService.selectOneReturnRecord(id);
        model.addAttribute("record",record);
        return "record/returnoperation";
    }

    @PostMapping("/homepage/returnoperationb")
    public String returnoperationb(Record record, Model model) {
        Integer integer = returnRecordService.updateReturnRecord(record);
        if (integer!=null){
            model.addAttribute("record",record);
            model.addAttribute("msg","修改成功");
            return "record/returnoperation";
        }else {
            model.addAttribute("msg","没有这条书籍");
            return "record/returnoperation";
        }
    }
}
